package leetcode.easy.limit50;

import java.util.Arrays;

public class No34 {
    public static void main(String[] args) {

        No34 no34 = new No34();
        int[] ints = no34.searchRange(new int[]{1}, 0);
        System.out.println(Arrays.toString(ints));

    }

    public int[] searchRange(int[] nums, int target) {
        int left = 0;
        int right = nums.length - 1;
        int[] result = new int[]{-1, -1};
        while (left <= right) {
            int mid = left + (right - left) / 2;
            if (nums[mid] == target) {
                right = mid - 1;
            } else if (nums[mid] > target) {
                right = mid - 1;
            } else if (nums[mid] < target) {
                left = mid + 1;
            }
        }
        if (left >= 0 && left < nums.length) {
            result[0] = nums[left] == target ? left : -1;
        }

        left = 0;
        right = nums.length - 1;
        while (left <= right) {
            int mid = left + (right - left) / 2;
            if (nums[mid] == target) {
                left = mid + 1;
            } else if (nums[mid] > target) {
                right = mid - 1;
            } else if (nums[mid] < target) {
                left = mid + 1;
            }
        }
        if (right >= 0 && right < nums.length) {
            result[1] = nums[right] == target ? right : -1;
        }
        return result;
    }
}
